package cn.zwxky.android.staff;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbAppManager;
import com.zgscwjm.lsfbbasetemplate.LsfbFragmentActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;

import java.util.HashMap;

import cn.zwxky.android.staff.Approve.ApproveFragment;
import cn.zwxky.android.staff.BusinessAddList.BusinessAddListFragment;
import cn.zwxky.android.staff.Follow.FollowFragment;
import cn.zwxky.android.staff.HomePage.HomePageFragment;
import cn.zwxky.android.staff.My.MyDetailsBean;
import cn.zwxky.android.staff.My.MyDetailsEvent3;
import cn.zwxky.android.staff.My.MyFragment;
import cn.zwxky.android.staff.Utils.URLString;


/**
 * 主页面内容
 * Created by dm on 16-1-19.
 */
public class IndexActivity2 extends LsfbFragmentActivity {
    // 定义5个Fragment对象
    private FollowFragment fg1;
    private BusinessAddListFragment fg2;
    private HomePageFragment fg3;
    private ApproveFragment fg4;
    private MyFragment fg5;
    // 帧布局对象，用来存放Fragment对象
    private FrameLayout frameLayout;
    // 定义每个选项中的相关控件
    private ImageView firstImage;
    private ImageView secondImage;
    private ImageView thirdImage;
    private ImageView fourthImage;
    private ImageView zhongImage;
    private TextView firstText;
    private TextView secondText;
    private TextView thirdText;
    private TextView fourthText;
    // 定义FragmentManager对象管理器
    private FragmentManager fragmentManager;

    private ImageView img_badgview1;
    private ImageView img_badgview2;


    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1:
                    img_badgview1.setVisibility(View.GONE);
                    break;
                case 2:
                    img_badgview1.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    img_badgview2.setVisibility(View.GONE);
                    break;
                case 4:
                    img_badgview2.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_main_lay);

        ViewInjectUtils.inject(this);
        LsfbEvent.getInstantiation().register2(this);

        fragmentManager = getSupportFragmentManager();
        initView(); // 初始化界面控件
        setChioceItem(2); // 初始化页面加载时显示第一个选项卡
    }

    /**
     * 初始化页面
     */
    public void initView() {

// 初始化底部导航栏的控件
        firstImage = (ImageView) findViewById(R.id.first_image);
        secondImage = (ImageView) findViewById(R.id.second_image);
        thirdImage = (ImageView) findViewById(R.id.third_image);
        fourthImage = (ImageView) findViewById(R.id.fourth_image);
        zhongImage = (ImageView) findViewById(R.id.zhong_image);
        firstText = (TextView) findViewById(R.id.first_text);
        secondText = (TextView) findViewById(R.id.second_text);
        thirdText = (TextView) findViewById(R.id.third_text);
        fourthText = (TextView) findViewById(R.id.fourth_text);

        img_badgview1 = (ImageView) findViewById(R.id.index_img_badgview1);
        img_badgview2 = (ImageView) findViewById(R.id.index_img_badgview2);

        getData();

    }


    @OnClick({R.id.first_layout, R.id.second_layout, R.id.third_layout, R.id.fourth_layout, R.id.zhong_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.first_layout:
                setChioceItem(0);
                break;
            case R.id.second_layout:
                setChioceItem(1);
                break;
            case R.id.zhong_layout:
                setChioceItem(2);
                break;
            case R.id.third_layout:
                setChioceItem(3);
                break;
            case R.id.fourth_layout:
                setChioceItem(4);
                break;
        }
    }


    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.INFO, hashMap, MyDetailsBean.class, new MyDetailsEvent3<MyDetailsBean>(), true, true);
    }

    @EventAnnotation
    public void MyDetailsEvent(MyDetailsEvent3<MyDetailsBean> event) {
        switch (event.getCode()) {
            case 200:
                if (event.getData().getF_num().equals("0") && event.getData().getT_num().equals("0")) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(2);
                }

                if (event.getData().getMl_num().equals("0") && event.getData().getMs_num().equals("0")) {
                    handler.sendEmptyMessage(3);
                } else {
                    handler.sendEmptyMessage(4);
                }


                break;
            default:
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void tabOnClick(View view, ViewPager viewPager) {

    }

    /**
     * 设置点击选项卡的事件处理
     *
     * @param index 选项卡的标号：0, 1, 2, 3,4
     */
    private void setChioceItem(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        clearChioce(); // 清空, 重置选项, 隐藏所有Fragment
        hideFragments(fragmentTransaction);
        switch (index) {
            case 0:
                firstImage.setImageResource(R.drawable.bottom_img_follow_checked);
                firstText.setTextColor(getResources().getColor(R.color.theme_color));
                // 如果fg1为空，则创建一个并添加到界面上
                if (fg1 == null) {
                    fg1 = new FollowFragment();
                    fragmentTransaction.add(R.id.content, fg1);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(fg1);
                }
                break;
            case 1:
                secondImage.setImageResource(R.drawable.bottom_img_uploading_checked);
                secondText.setTextColor(getResources().getColor(R.color.theme_color));
                if (fg2 == null) {
                    fg2 = new BusinessAddListFragment();
                    fragmentTransaction.add(R.id.content, fg2);
                } else {
                    fragmentTransaction.show(fg2);
                }
                break;
            case 2:
                zhongImage.setImageResource(R.drawable.bottom_dingwei);
                if (fg3 == null) {
                    fg3 = new HomePageFragment();
                    fragmentTransaction.add(R.id.content, fg3);
                } else {
                    fragmentTransaction.show(fg3);
                }
                break;
            case 3:
                thirdImage.setImageResource(R.drawable.bottom_img_approve_checked);
                thirdText.setTextColor(getResources().getColor(R.color.theme_color));
                if (fg4 == null) {
                    fg4 = new ApproveFragment();
                    fragmentTransaction.add(R.id.content, fg4);
                } else {
                    fragmentTransaction.show(fg4);
                }
                break;
            case 4:
                fourthImage.setImageResource(R.drawable.bottom_img_my_checked);
                fourthText.setTextColor(getResources().getColor(R.color.theme_color));
                if (fg5 == null) {
                    fg5 = new MyFragment();
                    fragmentTransaction.add(R.id.content, fg5);
                } else {
                    fragmentTransaction.show(fg5);
                }
                break;
        }
        fragmentTransaction.commit(); // 提交
        getData();
    }

    /**
     * 当选中其中一个选项卡时，其他选项卡重置为默认
     */
    private void clearChioce() {
        firstImage.setImageResource(R.drawable.bottom_img_follow_check);
        firstText.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
        secondImage.setImageResource(R.drawable.bottom_img_uploading_check);
        secondText.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
        thirdImage.setImageResource(R.drawable.bottom_img_approve_check);
        thirdText.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
        fourthImage.setImageResource(R.drawable.bottom_img_my_check);
        fourthText.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
        zhongImage.setImageResource(R.drawable.bottom_dingwei);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    /**
     * 隐藏Fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (fg1 != null) {
            fragmentTransaction.hide(fg1);
        }
        if (fg2 != null) {
            fragmentTransaction.hide(fg2);
        }
        if (fg3 != null) {
            fragmentTransaction.hide(fg3);
        }
        if (fg4 != null) {
            fragmentTransaction.hide(fg4);
        }
        if (fg5 != null) {
            fragmentTransaction.hide(fg5);
        }
    }


    //点击退出弹出确认提示框
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            dialog();
            return true;
        }
        return true;
    }

    protected void dialog() {

//    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//    设置Title的图标
//        builder.setIcon(R.mipmap.ic_launcher);
//    设置Title的内容
        builder.setTitle("温馨提示");
//    设置Content来显示一个信息
        builder.setMessage("确定要退出应用吗？");
//    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
                LsfbAppManager.getAppManager().finishAllActivity();
                System.exit(0);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
//    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
//    显示出该对话框
        builder.show();
    }


    @Override
    protected void onDestroy() {
        LsfbEvent.getInstantiation().unregister2(this);
        super.onDestroy();
    }

}